﻿Shader "URP/Outline"
{
    Properties
    {
        _OutlineColor("Outline Color", Color) = (0, 0, 0, 1)
        _Outline ("Outline", Range(0, 2)) = 0.1
    }
    SubShader
    {
        Tags {
            "RenderType" = "Opaque"
            "IgnoreProjector" = "True"
            "UniversalMaterialType" = "Unlit"
            "RenderPipeline" = "UniversalPipeline"
        }
        LOD 100
        Cull Front
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha
        // offset - 1, - 1

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Atributes
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
            };

            CBUFFER_START(UnityPerMaterial)
            real4 _OutlineColor;
            real _Outline;
            CBUFFER_END

            Varyings vert (Atributes v)
            {
                Varyings o = (Varyings)0;
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);

                //法线转到屏幕坐标
                float3 vNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normalOS));
                //再转到裁切坐标
                float2 projPos = normalize(mul((float2x2)UNITY_MATRIX_P, vNormal.xy));

                //转到视空间
                //float3 vNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.tangleOS));
                //转到投影空间
                //float2 projPos =  normalize(mul((float2x2)UNITY_MATRIX_P, vNormal.xy));


                o.positionCS.xy += projPos * _Outline * 0.1;
                return o;
            }

            real4 frag (Varyings i) : SV_Target
            {
                real4 color = _OutlineColor;
                // color.a = 0.5 + 0.5 * sin(_Time.y * 5);
                return color;
            }
            ENDHLSL
        }
    }
}

